﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using InternshipManagement.Models;
using InternshipManagement.Helpers;

namespace InternshipManagement.Controllers
{
    public class UserPrivilegeController : Controller
    {
        AccountRepository accountRepository = new AccountRepository();
        DashboardRepository dashboardRepository = new DashboardRepository();
        //
        // GET: /UserPrivilege/

        [CustomAuthorize(Privileges = UserPrivileges.PrivelegeManagement)]
        public ActionResult Index()
        {
            UserPriviligeModel model = new UserPriviligeModel();
            model.Privileges = accountRepository.FindAllPrivileges().ToList();
            model.SecretaryPrivileges = accountRepository.FindAllUserPrivilegesIDs(accountRepository.GetUserTypeID("Secretary"));
            model.CompanyPrivileges = accountRepository.FindAllUserPrivilegesIDs(accountRepository.GetUserTypeID("Company"));
            model.StudentPrivileges = accountRepository.FindAllUserPrivilegesIDs(accountRepository.GetUserTypeID("Student"));
            return View(model);
        }

        [HttpPost]
        [CustomAuthorize(Privileges = UserPrivileges.PrivelegeManagement)]
        public ActionResult Index(FormCollection formvalues)
        {
            string[] UserTypes = new string[] {"Secretary","Company","Student" };
            foreach (string userType in UserTypes)
            {
                int UserTypeID = accountRepository.GetUserTypeID(userType);
                List<int> dashboardIDs = new List<int>();
                accountRepository.RemoveAllUserPrivilege(UserTypeID);
                dashboardRepository.RemoveAllByUserTypeID(UserTypeID);
                foreach (Privilege p in accountRepository.FindAllPrivileges().ToList())
                {
                    if (formvalues[userType + "-" + p.ID] == "True")
                    {
                        UserPrivilege userPrivilege = new UserPrivilege();
                        userPrivilege.PrivilegeID = p.ID;
                        userPrivilege.UserTypeID = UserTypeID;
                        accountRepository.AddUserPrivilege(userPrivilege);
                        if(!dashboardIDs.Contains(p.DashboardID.Value))
                            dashboardIDs.Add(p.DashboardID.Value);
                    }
                }
                foreach (int id in dashboardIDs)
                {
                    if(id%10 == 0 ||  !dashboardIDs.Contains((id/10)*10))
                    {
                        DashboardAuth d = new DashboardAuth();
                        d.UserTypeID = UserTypeID;
                        d.DashboardID = id;
                        dashboardRepository.AddDashboardAuth(d);
                    }
                }
            }
            accountRepository.Save();
            dashboardRepository.Save();
            UserPriviligeModel model = new UserPriviligeModel();
            model.Privileges = accountRepository.FindAllPrivileges().ToList();
            model.SecretaryPrivileges = accountRepository.FindAllUserPrivilegesIDs(accountRepository.GetUserTypeID("Secretary"));
            model.CompanyPrivileges = accountRepository.FindAllUserPrivilegesIDs(accountRepository.GetUserTypeID("Company"));
            model.StudentPrivileges = accountRepository.FindAllUserPrivilegesIDs(accountRepository.GetUserTypeID("Student"));
            return View(model);
        }
    }
}
